package com.allianz.webapps.controllers;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.webapps.https.interfaces.quoSearch.QuoSearch;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.https.interfaces.util.PingYinUtil;
import com.allianz.webapps.quopojo.QuoBase;
import com.allianz.webapps.quopojo.QuoCriteria;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

//http://localhost:8080/pad_marketing/main/quoSearch.action
@Controller
@RequestMapping(value="main")
public class QuoSearchController {

	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private QuoSearch quoSearch;
	
	@RequestMapping(value="quoSearch")
	@ResponseBody
	public List<QuoBase> quoSearch(@RequestParam(value="jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response)
			throws JsonProcessingException, Exception{
		QuoCriteria quocri=objectMapper.readValue(jsonParam,QuoCriteria.class);
		//从session里边获取uesr
		User user=(User)request.getSession().getAttribute("username");
		quocri.setUser(user);
		String body=quoSearch.requestSearchParam(quocri);
		JSONObject json=new JSONObject(body);
		ObjectMapper mapper=new ObjectMapper();
		if(json.get("Body").equals(null)){
			if(!"S".equals(json.getString("Status")))
					throw new Exception(json.getString("ErrMsg"));
			else
				return null;	
		}
		String body1=json.getString("Body");
		List<QuoBase> quoBases=mapper.readValue(body1,new TypeReference<List<QuoBase>>(){});
		return quoBases;
	}
	
	@RequestMapping(value="getQuoByName")
	@ResponseBody
	public List<QuoBase> getQuoByName(@RequestParam(value="jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response)
			throws JsonProcessingException, Exception{
		QuoCriteria quocri=objectMapper.readValue(jsonParam,QuoCriteria.class);
		//从session里边获取uesr
		User user=(User)request.getSession().getAttribute("username");
		quocri.setUser(user);
		String body=quoSearch.requestSearchParam(quocri);
		JSONObject json=new JSONObject(body);
		ObjectMapper mapper=new ObjectMapper();
		if(json.get("Body").equals(null)){
			if(!"S".equals(json.getString("Status")))
					throw new Exception(json.getString("ErrMsg"));
			else
				return null;	
		}
		String body1=json.getString("Body");
		List<QuoBase> quoBases=mapper.readValue(body1,new TypeReference<List<QuoBase>>(){});
		for(int i = 0; i < quoBases.size(); i++) {
			quoBases.get(i).setHeadLetter(PingYinUtil.getFirstSpell(quoBases.get(i).getQuoName()).substring(0, 1).toUpperCase());
			char letter = quoBases.get(i).getHeadLetter().charAt(0);
			if(letter <'A' || letter > 'Z') {
				quoBases.get(i).setHeadLetter("#");
			}
		}
		return quoBases;
	}
}
